

package dao

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import entity.Custody

public class CustodyDAO implements DAO <Custody> {
    
    private Integer ID = 0;
    
    private static final String SQL_INSERT = " INSERT INTO Custody ( status, date, amount, employee, coments ) VALUES ( ?, ?, ?, ?, ?)";
    
    private static final String SQL_SELECT = " SELECT * FROM Custody where id= ?";
    
    private static final String SQL_SELECT_PROPERTY = " SELECT * FROM Custody where ? = ?" ;
    
    private static final String SQL_SELECT_All = " SELECT * FROM Custody";
    
    private static final String SQL_UPDATE = " UPDATE Custody SET id = ?, status = ? , date = ? , amount = ? , employee = ? , coments = ? WHERE  id = ? ";

    private static final String SQL_DELETE = " DELETE FROM Custody WHERE id = ?";
    
    //Ignore the error, Netbeans regards it like so but it's not.
    
    Integer create(Custody cust, Connection connect) 
        throws SQLException {

        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_INSERT,Statement.RETURN_GENERATED_KEYS);
            //ps.setInt(1, Custody.getId());
            ps.setBoolean(1, cust.getStatus());
            ps.setDate(2, cust.getDate());
            ps.setFloat(3,cust.getAmount());
            ps.setInt(4,cust.getResponsible().getID());
            ps.setString(5,cust.getComments());
            ID = ps.executeUpdate();
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Boolean remove(Custody cust, Connection connect) throws SQLException {
        Boolean result= false;
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_DELETE);
            ps.setInt(1, cust.getId());
            ps.executeUpdate();
            result = true;
        } finally {
            close(ps);
        }
        return result;
        //TODO implement
    }
    
    Boolean update(Custody cust, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_UPDATE,Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, cust.getId());
            ps.setBoolean(2, cust.getStatus());
            ps.setDate(3, cust.getDate());
            ps.setFloat(4,cust.getAmount());
            ps.setInt(5,cust.getResponsible().getID());            
            ps.setString(6,cust.getComments());
            ps.setInt(7, cust.getId());
            ID = ps.executeUpdate();
            
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Custody find(Integer id, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT);
            ps.setString(1, id);
            rs = ps.executeQuery();
            Custody cust = new Custody();
            result = null;
            cust = get_custody(rs);
            return cust;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List findAll(Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_All);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Custody cust = new Custody();
            result = null;
            while (rs.next()) {
                cust = get_custody(rs);
                results.add(cust);}
            return result;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List select_by_property(String camp, String Value, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_PROPERTY);
            ps.setString(1, camp);
            ps.setString(2, value);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Custody cust = new Custody();
            result = null;
            while (rs.next()) {
                cust = get_custody(rs);
                results.add(cust);}
            return result;

        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
        
    Custody get_custody (ResultSet rs) throws SQLException {
 
        Custody cust = new Custody();
        
        cust.setId(rs.getInt("id"));
        cust.setStatus(rs.getString("status"));
        cust.setDate(re.getDate("date"));
        cust.setAmount(rs.getFloat("amount"));
        cust.setInt(rs.getInt("employeeId"));            
        cust.setComments(rs.getString("comments"));

        return cust;
    }
}

